package com.fjh.shop.goods.service.service.impl;

import com.fjh.shop.goods.model.vo.CategoryListVO;
import com.fjh.shop.goods.model.entity.Category;
import com.fjh.shop.goods.service.mapper.CategoryMapper;
import com.fjh.shop.goods.service.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-16
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public Page<Category> pageQuery(Integer current, Integer pageSize, String name, Integer isShow) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        // 分类名模糊查询
        queryWrapper.like(StringUtils.hasText(name), Category::getName, name);
        // 状态查询
        queryWrapper.eq(isShow != null, Category::getIsShow, isShow);

        // 分页查询
        return this.page(new Page<>(current, pageSize), queryWrapper);
    }

    @Override
    public List<CategoryListVO> getList() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getIsShow, 1);
        queryWrapper.orderByDesc(Category::getSort);
        List<Category> categoryList = this.list(queryWrapper);

        List<CategoryListVO> categoryListVOList = new ArrayList<>();
        // 将CategoryListVO对象返回
        for (Category category : categoryList) {
            CategoryListVO categoryListVO = new CategoryListVO(category);
            categoryListVOList.add(categoryListVO);
        }

        return categoryListVOList;
    }
}
